package br.com.vti.sisp_sicrim.controller;

import javax.servlet.http.HttpSession;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.crud4j.core.util.Message;
import br.com.crud4j.core.util.MessageResolver;

@Resource
@Path("/auten")
public class AutenticacaoController {

    private final Result result;
    private final HttpSession  session;
    private final MessageResolver messageResolver;

    public AutenticacaoController(Result result, HttpSession session, MessageResolver messageResolver) {
        this.result = result;
        this.session = session;
        this.messageResolver = messageResolver;
    }

    protected Result getResult() {
        return result;
    }

    public HttpSession getSession() {
        return session;
    }

    protected MessageResolver getMessageResolver() {
        return messageResolver;
    }

    @Get
    public void index() {
    }
    
    @Get
    public void login() {
        getResult().include("mensagem", getErrorMessage(getSession()));
    }
    
    @Get("acessoNegado")
    public void acessoNegado() {
    }
    
    private Message getErrorMessage(HttpSession session) {
        Message erro = null;
        Exception ex = (Exception) session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        if (ex != null) {
//            if (ex instanceof BadCredentialsException) {
//                erro = getMessageResolver().getErrorMessage(getMessageResolver().getMessage("erro.usuario.invalido"));
//            } else {
//                Throwable causa = ex.getCause();
//                erro = getMessageResolver().getErrorMessage(getMessageResolver().getMessage(causa.getMessage()));
//            }
        }
        return erro;
    }
}
